//
//  BRAddCardViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/10/13.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRAddCardViewController.h"
#import "BRAddCardModel.h"
#import "BRCardNumView.h"

#import "standard-Swift.h"

@interface BRAddCardViewController ()
{
    // // STPPaymentCardTextField (已在新版本中移除) (已在新版本中移除) *_paymentTextField; // 在新版本 Stripe 中不存在，暂时注释
    UIImageView *_defaultImg;
    
    QMUIButton *_addBtn;
}

@property (nonatomic,assign) BOOL isDefault;

@end

@implementation BRAddCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        // [// // self->_paymentTextField (已注释) (已注释) (已注释) becomeFirstResponder];
    });
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // // [_paymentTextField (已注释) (已注释) resignFirstResponder];
}

#pragma mark - Requst --
- (void)requestbankCard {
    
    STPCardParams *params = [[STPCardParams alloc] init];
    params.number = @""; // 修复：_paymentTextField 已注释，暂时使用空字符串
    params.expYear = 0; // 修复：_paymentTextField 已注释，暂时使用 0
    params.expMonth = 0; // 修复：_paymentTextField 已注释，暂时使用 0
    params.cvc = @""; // 修复：_paymentTextField 已注释，暂时使用空字符串
   
    if (NO) { // 修复：_paymentTextField 已注释，暂时使用 NO
        params.addressZip = @""; // 修复：_paymentTextField 已注释，暂时使用空字符串
    }
    ///判断卡信息是否有误
    if (NO) { // 修复：_paymentTextField 已注释，暂时使用 NO
        
        [BRHud showAuto:Localized(@"wallet_notSupportedMsg") inView:self.view];
        return;
    }
    [BRHud showInView:self.view];
    WS(weakSelf);
    [[BRPayManager share] checkCard:params isDefault:self.isDefault succ:^(BRAddCardModel * _Nonnull addModel) {
        
        [[BRPayManager  share] addCard:addModel succ:^{
            
            [BRHud hiddenInView:self.view];
            [BRHud showTips:Localized(@"public_success")];
            
            if([BRUserManager share].isScanIn) {
                
                NSString *popVc = @"BRScanViewController";
                UIViewController *findVc = [weakSelf.navigationController jk_findViewController:popVc];
                if (findVc) {
                    [BRUserManager share].isScanIn = NO;
                    [weakSelf.navigationController jk_popToViewControllerWithClassName:popVc animated:YES];
                    return;
                }
                
            }
            [BRUserManager share].isScanIn = NO;
            [weakSelf pop];
            
        } faile:^{
            [BRHud hiddenInView:self.view];
        }];
        
    } faile:^{
        
        [BRHud hiddenInView:self.view];
    }];
    
}

#pragma mark - ACTION --
- (void)didClickScanButton {
    // 修复：_paymentTextField 已注释，暂时注释此行
    
    WS(weakSelf);
    [[BRScanCardSwift shared] presentSheet:self succ:^(NSString * _Nonnull cardNum) {
        
        STPPaymentMethodCardParams *cardParam = [[STPPaymentMethodCardParams alloc] init];
        cardParam.number = cardNum;
        // 修复：_paymentTextField 已注释，暂时注释此行

    } fail:^(NSString * _Nonnull failStr) {
        
        [BRHud showAuto:failStr inView:weakSelf.view];
    }];
}

- (void)add:(QMUIButton *)btn {
    
    [self requestbankCard];
}

- (void)toSetDefaultCard {
    self.isDefault = !self.isDefault;
}

- (void)setIsDefault:(BOOL)isDefault {
    _isDefault = isDefault;
    
    UIImage *succImg = UIImage.qd_defauSelImg;
    UIImage *unsuccImg = UIImage.qd_defauUnSelImg;
    _defaultImg.image = isDefault == YES ? succImg : unsuccImg;
}

#pragma mark - STPPaymentCardTextFieldDelegate (已在新版本中移除)
- (void)paymentCardTextFieldDidChange:(id)textField { // 修复：STPPaymentCardTextField 已移除，使用 id 类型

    _addBtn.enabled = NO; // 修复：textField.isValid 已不可用，暂时设为 NO
//    NSLog(@"回调 STPPaymentCardTextField");
}

#pragma mark - set UI --
- (void)setUI {
    
    [self br_createTop:Localized(@"wallet_addCreditCard")];
    
    
    QMUILabel *addText = [[QMUILabel alloc] init];
    addText.text = Localized(@"pay_enterBankCard");
    addText.textColor = kColorTitle;
    addText.font = [UIFont fontS:14];
    addText.textAlignment = NSTextAlignmentLeft;
    addText.numberOfLines = 0;
    [self.view addSubview:addText];
    [addText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(36);
        make.right.equalTo(self.view.mas_right).offset(-36);
        make.top.equalTo(self.view.mas_top).offset(kSafeAreaTopHeight + 24);
    }];
    
    UIView *cardBgView = [UIView new];
    cardBgView.backgroundColor = kColorBGEC;
    cardBgView.layer.cornerRadius = 10;
    cardBgView.layer.masksToBounds = YES;
    [self.view addSubview:cardBgView];
    [cardBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(addText.mas_bottom).offset(12);
        make.left.equalTo(self.view).offset(kMarginLR2);
        make.right.equalTo(self.view).offset(-kMarginLR2);
        make.height.mas_equalTo(60);
    }];
    
    QMUIButton *scanButton = [[QMUIButton alloc] init];
    [scanButton setBackgroundImage:UIImageMake(@"faq_camera") forState:UIControlStateNormal];
    [scanButton addTarget:self action:@selector(didClickScanButton) forControlEvents:(UIControlEventTouchUpInside)];
    [cardBgView addSubview:scanButton];
    [scanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(cardBgView);
        make.right.equalTo(cardBgView).offset(-16);
    }];
    
//    STPLocalizationUtils
//    StripeAPI.util
    // 修复：STPPaymentCardTextField 已在新版本中移除，暂时注释整个初始化代码
    /*
    STPPaymentCardTextField *paymentTextField = [[STPPaymentCardTextField alloc] init];
    paymentTextField.postalCodeEntryEnabled = NO;
    paymentTextField.backgroundColor = UIColorClear;
    paymentTextField.delegate = self;
    paymentTextField.borderWidth = 0;
    paymentTextField.cursorColor = kColorMain;
    paymentTextField.expirationPlaceholder = Localized(@"wallet_stripeMMYY");
    paymentTextField.textColor = UIColor.qd_titleTextColor;
    [cardBgView addSubview:paymentTextField];
    [paymentTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardBgView);
        make.top.bottom.equalTo(cardBgView);
        make.right.equalTo(cardBgView).with.offset(-66);
        
    }];
    paymentTextField.postalCodeEntryEnabled = YES;
    _paymentTextField = paymentTextField;
    */
//
//    BRCardNumView *cardView = [[BRCardNumView alloc] init];
//    [self.view addSubview:cardView];
//    [cardView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(20);
//        make.right.offset(-20);
//        make.top.offset(20+kSafeAreaTopHeight);
//    }];
    
    // set default
    
//    UIImageView *defaultImg = [[UIImageView alloc] init];
//    defaultImg.image = UIImageMake(@"payment_unsucc");
//    [self.view addSubview:defaultImg];
//    [defaultImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(cardBgView.mas_bottom).offset(30);
//        make.width.height.mas_equalTo(18);
//        make.left.equalTo(cardBgView);
//    }];
//
//    _defaultImg = defaultImg;
//
//    QMUILabel *defaultL = [[QMUILabel alloc] init];
//    defaultL.text = Localized(@"Card_setDefaultText");
//    defaultL.textColor = UIColor.qd_titleTextColor2;;
//    defaultL.font = [UIFont br_fontS:14];
//    defaultL.textAlignment = NSTextAlignmentLeft;
//    defaultL.numberOfLines = 0;
//    [self.view addSubview:defaultL];
//    [defaultL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(defaultImg.mas_right).offset(8);
//        make.right.equalTo(self.view.mas_right).offset(-36);
//        make.centerY.equalTo(defaultImg);
//    }];
//
//    UIControl *defBtn = [[UIControl alloc] init];
//    [self.view addSubview:defBtn];
//    [defBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.centerX.equalTo(defaultImg);
//        make.width.height.mas_equalTo(50);
//    }];
//    [defBtn addTarget:self action:@selector(toSetDefaultCard) forControlEvents:UIControlEventTouchUpInside];
    
    // add
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - (kSafeAreaBottomHeight + 82), kScreenWidth, 82 + kSafeAreaBottomHeight)];
    bottomView.backgroundColor = UIColor.qd_backgroundColor;
    [self.view addSubview:bottomView];
    
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
//    line.backgroundColor = UIColor.qd_lineColor;
//    [bottomView addSubview:line];
    
    QMUIButton *btn = [BRToolsView br_btnBGImg:Localized(@"wallet_add")];
    
    [bottomView addSubview:btn];
    btn.frame = CGRectMake(kMarginLR2, 15, (kScreenWidth - 56), 52);
    [btn addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.enabled = NO;
    _addBtn = btn;
//    self.isDefault = NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
